<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
    const domains = [
      'asdfasfdasfa.com',
      '1232542345123345.com',
      'backup.com'
    ];

    const retry = {};
    window.addEventListener('error', (e) => {
      console.info('error....', e);
      // 脚本错误
      if (e instanceof ErrorEvent || e.target.tagName !== 'SCRIPT') {
        return;
      }

      const url = new URL(e.target.src);
      const pathname = url.pathname;

      if (!(pathname in retry)) {
        retry[pathname] = 0;
      }

      const index = retry[pathname];
      if (index >= domains.length) {
        return;
      }
      const newDomain = domains[index];

      url.host = newDomain;

      // 会阻塞页面
      // document.write(`\<script src="${url.toString()}"><\/script>`);

      const script = document.createElement('script');
      script.src = url.toString();
      e.target.parentElement.insertBefore(script, e.target);
      retry[pathname]++;
      e.target.remove();
    }, true);

  </script>
</head>

<body>
  <script src="http://static.com/js/1.js"></script>
  <script src="http://abcdefgasdf.com/js/2.js"></script>
  <script src="http://static.com/js/3.js"></script>
</body>

</html>